<?php
class Database{
    protected $host;
    protected $username;
    protected $password;
    protected $db;
    public function __construct($host=null,$username=null,$password=null,$db=null){
        if(isset($host))
            $this->host = $host;
        else
            $this->host = Config::DB_HOST;
            
        if(isset($username))
            $this->username = $username;
        else
            $this->username = Config::DB_USER;
        
        if(isset($password))
            $this->password = $password;
        else
            $this->password = Config::DB_PASSWORD;
        
        if(isset($db))
            $this->db = $db;
        else
            $this->db = Config::DB_NAME;
    }
    
    private function connect(){
        $con = mysql_connect($this->host,$this->username,$this->password ) or die("Cannot connect to Host");
        mysql_select_db($this->db) or die("Cannot connect to DB Name");
        return $con;
    }
    
    public function escape($str){
        return "'" . addslashes($str) . "'";
    }
    
    public function query($sql,$fetch_type="array"){
        $con = $this->connect();
        $query = mysql_query($sql);
        $data = array();
        if($fetch_type === "array"){
            while($row = mysql_fetch_assoc($query)){
                $data[] = $row;
            }
        }
        else if($fetch_type === "row"){
            $data = mysql_fetch_assoc($query);
        }
        else if($fetch_type === "update"){
            $data = mysql_affected_rows($con);
        }
        else if($fetch_type === "insert"){
            $data = mysql_insert_id($con);
        }
        else{return false;}
        mysql_close($con);
        return $data;
    }
    
    public function update($table,$fields=array(),$conditions=array()){
        $field_string = ""; $cond_string="";
        foreach($fields as $field => $value){
            $value = is_numeric($value) ? $value : $this->escape($value); 
            $field_string .= $field . "=" . $value .  ",";
        }
        foreach($conditions as $condition => $value){
            $value = is_numeric($value) ? $value : $this->escape($value); 
            $cond_string .= $condition . "=" . $value .  ",";
        }
        $field_string = substr($field_string,0,strlen($field_string) -1 );
        $cond_string = substr($cond_string,0,strlen($cond_string) -1 );
        $query = $this->query("UPDATE {$table} SET {$field_string} WHERE {$cond_string}","update");
        return $query;
    }
    
    public function insert($table,$fields=array()){
        $field_string = ""; 
        foreach($fields as $field => $value){
            $value = is_numeric($value) ? $value : $this->escape($value); 
            $field_string .= $field . "=" . $value .  ",";
        }
        $field_string = substr($field_string,0,strlen($field_string) -1 );
        $query = $this->query("INSERT INTO {$table} SET {$field_string}","insert");
        return $query;
    }
    
    public function delete($table,$conditions=array()){
        $cond_string="";
        foreach($conditions as $condition => $value)
        {
            $value = is_numeric($value) ? $value : $this->escape($value); 
            $cond_string .= $condition . "=" . $value .  ",";
        }
        $cond_string = substr($cond_string,0,strlen($cond_string) -1 );
        $query = $this->query("DELETE FROM {$table} WHERE {$cond_string}","update");
        return $query;
    }
}